package com.spzx.product.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.spzx.common.core.web.controller.BaseController;
import com.spzx.common.core.web.domain.AjaxResult;
import com.spzx.common.core.web.page.TableDataInfo;
import com.spzx.common.security.utils.SecurityUtils;
import com.spzx.product.domain.Brand;
import com.spzx.product.service.BrandService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * <p>
 * 分类品牌 前端控制器
 * </p>
 *
 * @author atguigu
 * @since 2025-04-28
 */
@RestController
@RequestMapping("/brand")
public class BrandController extends BaseController {
    @Autowired
    BrandService brandService;

    @Operation(summary = "查询品牌列表")
    @GetMapping("/list")
    public TableDataInfo list(String name){
        startPage();
        List<Brand> list= brandService.selectbyid(name);
        return getDataTable(list);
    }

    @Operation(summary = "获取品牌详细信息")
    @GetMapping("/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id) {
        Brand byId = brandService.getById(id);
        return AjaxResult.success(byId);
    }

    @Operation(summary = "新增品牌")
    @PostMapping
    //前端传过来json数据接收用@Resquestbody来接收
    public void add(@Parameter(description = "品牌") @RequestBody Brand brand) {
        //SecurityUtils.getUsername()获得当前用户登录名称
        //使用brand.setCreateBy()方法将获取的用户名赋值给Brand对象的createBy属性
        brand.setCreateBy(SecurityUtils.getUsername());
        LambdaQueryWrapper<Brand> lambdaQueryWrapper=new LambdaQueryWrapper<>();
        lambdaQueryWrapper.eq(Brand::getName,brand.getName());

        if(brandService.count(lambdaQueryWrapper)>0){
            throw new RuntimeException("品牌已存在");
        }
        brandService.save(brand);

    }
    @Operation(summary = "修改品牌")
    @PutMapping
    public AjaxResult edit(@Parameter(description = "品牌") @RequestBody Brand brand) {
        //SecurityUtils.getUsername()获得当前用户登录名称
        //将获取到的用户名通过brand.setUpdateBy()方法设置到Brand对象的updateBy属性中
        brand.setUpdateBy(SecurityUtils.getUsername());
        //验证品牌是否存在
        LambdaQueryWrapper<Brand> lambdaQueryWrapper=new LambdaQueryWrapper<>();
        lambdaQueryWrapper.ne(Brand::getId,brand.getId());
        lambdaQueryWrapper.eq(Brand::getName,brand.getName());

        if (brandService.count(lambdaQueryWrapper)>0){
            throw new RuntimeException("品牌已存在");
        }

        return AjaxResult.success(brandService.updateById(brand));
    }


    @Operation(summary = "删除品牌")
    @DeleteMapping("/{ids}")
    public AjaxResult remove(@Parameter(description = "品牌id列表", example = "[1,2]") @PathVariable List<Long> ids) {
        brandService.removeByIds(ids);
        return AjaxResult.success();

    }

    @Operation(summary = "查询品牌信息")
    @GetMapping("/getBrandAll")
    public AjaxResult get(){
        List<Brand> list = brandService.list();
        return AjaxResult.success(list);
    }

}
